
<div class="main_bd">

    <div class="panel panel_overview">
        <div class="panel__hd">
            <h3 class="panel__title">拼团列表</h3></div>
        <div class="panel__bd">

            <div class="filter-box">
            <a href="/group/select" class="btn btn_primary">新建拼团</a>
            </div>
            <table class="list-box cell-border hover stripe" id="Table1">


                <tfoot>

                </tfoot>
            </table>
        </div>
    </div>
</div>
<script>

    function getValidate(status) {
        if(status==0)return '未提交审核'
        if(status==1)return '待审核'
        if(status==3)return '已审核'
        if(status==-1)return '未通过'
        return '状态异常';//万一还有其他的呢
    }

    function toValidate(id) {

        $.post('/group/update?id='+id,{validate:1},function(res){
            if(res.code==1){
                layer.msg('申请提交成功');
                pageIndex = grid.page();
                grid.page(pageIndex).draw('page');
            }else{
                layer.msg(res.msg);
            }
        })
    }

    $(function(){



        window.grid = $('#Table1').DataTable({
            serverSide: true,
            paging: true,//开启分页
            lengthMenu: [ //自定义分页长度
                [ 20, 50, 100 ],
                [ '20 页', '50 页', '100页' ]
            ],
            columns: [
                {"title": "货品id", 'data': 'id',width: "80px"},
                {"title": "名称", 'data': 'goods.title',width: "200px"},
                {"title": "缩略图", 'data': 'goods.thumb'},
                // {"title": "上架状态", 'data': 'status'},
                {"title": "审核状态", 'data': 'validate'},
                {"title": "拼团销量", 'data': 'sales',"defaultContent":"0"},
                {"title": "开始时间", 'data': 'totime'},
                {"title": "截止时间", 'data': 'endtime'},
                {"title": "操作", 'data': 'control',"defaultContent":""},
            ],
            "createdRow": function (row, data, index) {
                // console.log(data.validate)
                //
                // $('td', row).eq(-1).append($(tmpl));

            },
            "ajax": {
                url: "/group",
                type: "POST",
                data: function (d) {
                    //删除多余请求参数
                    for(var key in d){
                        if(key.indexOf("columns")==0||key.indexOf("order")==0||key.indexOf("search")==0){ //以columns开头的参数删除
                            delete d[key];
                        }
                    }
                    console.log(d)
                    var searchParams= {
                        page:d.draw,
                        size:d.length,
                        paginate:1,
                        with:'goods,info',
                    };
                    //附加查询参数
                    if(searchParams){
                        $.extend(d,searchParams); //给d扩展参数
                    }
                },
                dataType : "json",
                "dataSrc": function ( json ) {
                    json.data.map(function(rows){
                        rows.goods.thumb = '<img width="60px" src="'+rows.goods.thumb+'"/>';


                        var tmpl = '<a href="/group/edit?id='+rows.id+'"  class="btn btn_primary">修改</a>';
                        if(rows.validate<1){
                            tmpl += '<button onclick="toValidate('+rows.id+')"  style="margin-left: 10px" class="btn btn_warn" >提交审核</button>';
                        }

                        rows.control =tmpl;
                        rows.validate = getValidate(rows.validate);
                    })

                    return json.data;
                },
                dataFilter: function (json) {
                    //json是服务器端返回的数据
                    json = JSON.parse(json);
                    console.log(json)
                    var returnData = {};
                    // returnData.draw = json.data.current_page;
                    returnData.recordsTotal = json.data.total;//返回数据全部记录
                    returnData.recordsFiltered = json.data.total;//后台不实现过滤功能，每次查询均视作全部结果
                    returnData.data = json.data.data;//返回的数据列表
                    return JSON.stringify(returnData);//这几个参数都是datatable需要的，必须要
                }
            },
            oLanguage:{ // 国际化配置
                "sProcessing" : "正在获取数据，请稍后...",
                "sLengthMenu" : "显示 _MENU_ 条",
                "sZeroRecords" : "没有找到数据",
                "sInfo" : "从 _START_ 到  _END_ 条记录 总记录数为 _TOTAL_ 条",
                "sInfoEmpty" : "记录数为0",
                "sInfoFiltered" : "(全部记录数 _MAX_ 条)",
                "sInfoPostFix" : "",
                "sSearch" : "查询",
                "sUrl" : "",
                "oPaginate" : {
                    "sFirst" : "第一页",
                    "sPrevious" : "上一页",
                    "sNext" : "下一页",
                    "sLast" : "最后一页"
                }
            }
        });


    })


</script>